/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame;

import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.material.RenderState;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Cylinder;
import com.jme3.scene.shape.Sphere;

/**
 *
 * @author Eden
 */
public final class Bottle extends Node {
        public final static boolean USELIGHT = true;
        public final static boolean ISWIREFRAME = true;     
        private AssetManager assetManager;
        private float size;
        ColorRGBA color;
        private Geometry geoms[];

      public Bottle(AssetManager assetManager, float size, ColorRGBA color, boolean isWireframed)
        {
            this(assetManager, size, color, isWireframed, false);  // no light
        }     
      public Bottle(AssetManager assetManager, float size, ColorRGBA color,  boolean isWireframed, boolean useLight)
        {    
            super("Bottle");   // constructor Node
        
            this.assetManager = assetManager;
            this.size = size;     
            this.color = color;
            Sphere s =    new Sphere(32, 32, size);     // overgang hals body
            Cylinder c1 = new Cylinder(40, 50, size, 2.5f*size, true, false); // closed hals
            Cylinder c2 = new Cylinder(40, 50, 0.25f*size, size, true, false);                      
            initGeometry(new Geometry("Bol", s),        // init Positions Geometries
                         new Geometry("Cyl1", c1), new Geometry("Cyl2", c2));       
            if (useLight)
               initMaterialForLight();    // no wireframe.....possible
            else
                initMaterial(isWireframed);  // true is wireframe    
        }
         // zet de fles inelkaar 
        void initGeometry(Geometry... geoms)  // argument is een array !! 
        {   
            if (geoms.length == 3)      // test verwacht 3 objecten in de array
            {   
                this.geoms = geoms;
                this.geoms[0].rotate(FastMath.PI/2, 0, 0); // rechtop
                this.geoms[1].rotate(FastMath.PI/2, 0, 0);  
                this.geoms[2].rotate(FastMath.PI/2, 0, 0);
                this.geoms[0].setLocalTranslation(new Vector3f(0,0,0));
                this.geoms[1].setLocalTranslation(new Vector3f(0,-1.25f*size,0));
                this.geoms[2].setLocalTranslation(new Vector3f(0,1.25f*size,0));
                for (Geometry geom : geoms)     // after all we are a Node ;-)
                    attachChild(geom);         
            }
            else    System.err.println("error: need 3 Geometries objects");
        }
        
        void initMaterial(boolean isWireframed)
        {
          Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
          mat.setColor("Color", color); 
            if (isWireframed)
                mat.getAdditionalRenderState().setWireframe(true);  // extra
            for (int i = 0; i < 3; i++) 
            {
                geoms[i].setMaterial(mat);
            }
       }    
        void initMaterialForLight()
        {      
            Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
            mat.setBoolean("UseMaterialColors", true);
            mat.setColor("Ambient", color);
            mat.setColor("Diffuse", color);
            mat.setColor("Specular", ColorRGBA.White);
            mat.setFloat("Shininess", 8);    // [0,128]
            mat.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off);
            mat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);  
            for (int i = 0; i < 3; i++) 
            {
                /** Give geometry glass properties */
                geoms[i].setMaterial(mat);
            }
            
        }

}
